<?php
/**
 * 企业分类控制器
 * @author ThankPHP
 * @link Http://www.ThankPHP.Com
 * @copyright Power By ThankPHP
 */
class ExcategoryAction extends Action {
	/**
	 * 列出所有的分类
	 */
	public function AdminIndex(){
		EasyACL('Excategory','AdminIndex',$_SESSION);
		//设置查询条件
		$conditions = array();
		//只进行查询根分类,其它非根分类将进行关联查询
		$conditions['state'] = 0;
		$Ex = D('ecategory');
		$re = $Ex->field('id,cid,name')->order('id asc')->xfindAll($conditions);
		//设置到模板
		$this->assign('Class',$re);
		$this->display();
	}
	
	/**
	 * 创建新分类
	 */
	public function AdminCreate(){
		EasyACL('Excategory','AdminCreate',$_SESSION);
		//如果POST提交
		if ($this->isPost()){
			$Input = Input::getInstance();
			$Input->filter('addslashes');

			$Ex = D('ecategory');
			$Ex->cid = $Input->post('cid');
			$Ex->name = $Input->post('name');
			$Ex->note = $Input->post('note');
			$Ex->csort = $Input->post('csort');
			$Ex->state = ($Input->post('cid') == 0)?0:1;
			$Re = $Ex->add();
			if (!$Re){
				halt('尝试创建新的分类失败!');
			}
			$this->redirect('AdminIndex','Excategory');
		}
		//获取到所有的根分类,设置查询条件
		$conditions = array();
		$conditions['state'] = 0;
		
		$Ex = D('ecategory');
		$result = $Ex->where($conditions)->findAll();
		//设置到模板
		$this->assign('BigClass',$result);
		$this->display();
	}

	/**
	 * 修改分类
	 */
	public function AdminUpdate(){
		EasyACL('Excategory','AdminUpdate',$_SESSION);

		$Input = Input::getInstance();
		$Input->filter('addslashes');

		//如果POST提交
		if ($this->isPost()){
			//设置条件
			$conditions = array();
			$conditions['id'] = $Input->post('id');
			//获取数据
			$Ex = D('ecategory');
			$Ex->find($conditions);
			$Ex->cid = (int)$Input->post('cid');
			$Ex->name = htmlspecialchars($Input->post('name'));
			$Ex->note = htmlspecialchars($Input->post('note'));
			$Ex->csort = (int)$Input->post('csort');
			$Ex->state = ($Input->post('cid') == 0)?0:1;
			$Re = $Ex->save();
			if (!$Re){
				halt('修改分类失败');
			}
			$this->redirect('AdminIndex','Excategory');
		}
		
		//设置查询条件 - 进行查询所有的大分类
		$conditionOne = array();
		$conditionOne['state'] = 0;
		
		//设置查询条件
		$conditions = array();
		$conditions['id'] = $Input->get('id');
		
		//获取要修改的分类
		$Ex = D('ecategory');
		$result = $Ex->where($conditions)->find();
		$Ex = NULL;
		//获取所有的大分类
		$Ex = D('ecategory');
		$All_Big_Class = $Ex->where($conditionOne)->findAll();
		$Ex = NULL;
		//设置到模板文件
		$this->assign('Class',$result);
		$this->assign('BigClass',$All_Big_Class);
		$this->display();
	}

	/**
	 * 删除分类
	 */
	public function AdminDel(){
		EasyACL('Excategory','AdminDel',$_SESSION);
		//POST提交
		if ($this->isPost()){
			$Ex = D('ecategory');
			$Error = '';
			$id = $_POST['id'];
			foreach($id as $d){
				$Re = $Ex->delete($d);
				if (!$Re){
					$Error .= '尝试删除编号为' . $d . '的失败!<br />';
				}
			}
			
			if (empty($Error)) {
				$this->redirect('AdminIndex','Excategory');
			} else {
				halt('尝试删除选中的分类失败!');
			}
		}
		$this->redirect('AdminIndex','Excategory');
	}
	
	/**
	 * 进行获取指定的大分类下的信息
	 */
	public function AdminGetxClass(){
		//获取传入值
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		$Cid = (int)$Input->get('id');
		//进行获取相应的数据
		//设置查询条件
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		$conditions->put('id',array('EQ',$Cid));
		
		//进行查询
		$Ex = D('ecategory');
		$result = $Ex->where($conditions)->xfind();
		
		//设置到模板
		$this->assign('Exs',$result);
		$this->display();
	}
	
	
}